package com.anlu.base.reflection;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public class MyClass {
    private List<RefTest1.Student1> studentList;

    private List<String> names;

    // 从方法返回值创建
    public List<String> getNames () {
        return null;
    }
    // 带有泛型信息的字段

    @Test
    void testFieldGeneric() throws NoSuchFieldException {
        Field field = MyClass.class.getDeclaredField("studentList");
        Type genericType = field.getGenericType();

        if (genericType instanceof ParameterizedType) {
            ParameterizedType pt = (ParameterizedType) genericType;
            if (pt.getRawType() == List.class) {
                Type[] typeArgs = pt.getActualTypeArguments();
                Class<?> genericClass = (Class<?>) typeArgs[0];
                System.out.println("List的泛型类型: " + genericClass.getName());
            }
        }
    }
}
